<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/2/27
 * Time: 15:45
 */

namespace App\Api\Admin\Controllers;
use App\Models\User;
use  App\Models\Media;
use App\Models\MarketingMediaLike;
use App\Services\MediaLikeConfigService;
use Illuminate\Http\Request;
use App\Models\MediaLikeConfig;
use App\Models\MediaCommentConfig;
use App\Models\MediaCommentTemplate;
use App\Services\MediaCommentConfigService;
use App\Models\Admin\Log;

class PointSponsorController extends CommonController
{
    public function __construct()
    {
        parent::__construct();

    }

    public function index()
    {
        $frequency = IT('frequency',true,'请输入频率');
        $Interval_time = IT('Interval_time',true,'时间');
         $user_data= User::where([ 'status'=>1,'role'=>2])->get();

         $media_data =  Media::where(['status'=>1,'is_delete'=>0,'is_private' =>0])->get();
            foreach ($media_data as $key){
                $maeketing_data['media_id'] = $key->id;
                foreach ($user_data as $uid){
                    $maeketing_data['uid'] = $uid->id;
                    MarketingMediaLike::insert($maeketing_data);
                }
                $maeketing_data['created_at'] = time2date();
                MarketingMediaLike::insert($maeketing_data);
            }
        return json_success('ok');
    }

    /**
     * @desc 创建视频点赞配置
     * @return array
     */
    public function add_media_like_config()
    {
        $params = [];
        $params['config_name'] = IT('config_name', true, '请输入配置名称');
        $params['media_type'] = IT('media_type', true, '请选择点赞对象');
        $params['frequency'] = (int)IT('frequency', true, '请输入点赞的频率');
        $params['cycle_type'] = IT('cycle_type', true, '请输入点赞的周期类型');
        $params['cycle_time'] = (int)IT('cycle_time', 1);
        $params['start_time'] = IT('start_time', true, '请输入点赞的开始时间');
        $params['end_time'] = IT('end_time', true, '请输入点赞的结束时间');
        $params['media_id'] = IT('media_id');   // 视频ID，以,分隔
        $params['uid'] = IT('uid'); // 微影号
        $params['account_num'] = IT('account_num', true, '请输入营销账号数量');
        $params['operator_id'] = $this->user['id'];

        $rs = MediaLikeConfigService::add_media_like_config($params);
        if($rs['code'] != 0) {
            return json_error($rs['msg']);
        }

        (new Log)->add($this->user,'-创建视频点赞配置');
        return json_success('ok');
    }

    /**
     * @desc 修改视频点赞配置
     * @return array
     */
    public function modify_media_like_config()
    {
        $params = [];
        $params['id'] = IT('id', true, '请选择点赞配置');
        $params['config_name'] = IT('config_name', true, '请输入配置名称');
        $params['media_type'] = IT('media_type', true, '请选择点赞对象');
        $params['frequency'] = (int)IT('frequency', true, '请输入点赞的频率');
        $params['cycle_type'] = IT('cycle_type', true, '请输入点赞的周期类型');
        $params['cycle_time'] = (int)IT('cycle_time', 1);
        $params['start_time'] = IT('start_time', true, '请输入点赞的开始时间');
        $params['end_time'] = IT('end_time', true, '请输入点赞的结束时间');
        $params['media_id'] = IT('media_id');   // 视频ID，以,分隔
        $params['uid'] = IT('uid'); // 微影号
        $params['operator_id'] = $this->user['id'];
        $params['account_num'] = IT('account_num', true, '请输入营销账号数量');

        $rs = MediaLikeConfigService::modify_media_like_config($params);
        if($rs['code'] != 0) {
            return json_error($rs['msg']);
        }

        (new Log)->add($this->user,'-修改视频点赞配置');
        return json_success('ok');
    }

    /**
     * @desc 修改视频点赞配置状态
     * @return array
     */
    public function modify_media_like_config_status()
    {
        $params = [];
        $params['id'] = IT('id', true, '请选择点赞配置');
        $params['status'] = IT('status', true, '点赞配置状态异常');
        $params['operator_id'] = $this->user['id'];

        $rs = MediaLikeConfigService::modify_media_like_config_status($params);
        if($rs['code'] != 0) {
            return json_error($rs['msg']);
        }

        (new Log)->add($this->user,'-修改视频点赞配置状态');
        return json_success('ok');
    }

    /**
     * @desc 获取视频点赞配置列表
     * @return array
     */
    public function get_media_like_config_list(Request $request)
    {
        $page = max(1, (int) $request->input('page'));
        $page_size = (int) $request->input('pageszie');
        $page_size = $page_size ?: 20;
        $offset = ($page - 1) * $page_size;
        $total = MediaLikeConfig::where([])->count();
        $fields = ['id', 'config_name', 'media_type', 'frequency', 'cycle_type', 'cycle_time', 'status', 'start_time', 'end_time'];
        $config_list = MediaLikeConfig::where([])->skip($offset)->take($page_size)->orderBy('id', 'desc')->get($fields);
        $total_page = ceil($total / $page_size);
        if(!empty($config_list)) {
            foreach ($config_list as &$config) {
                $config['start_time'] = time2date($config['start_time']);
                $config['end_time'] = time2date($config['end_time']);
            }
        }

        return json_success('ok', ['totalpage' => $total_page,'total'=>$total, 'list' => $config_list]);
    }

    /**
     * @desc 获取视频点赞配置详情
     * @return array
     */
    public function get_media_like_config_info()
    {
        $id = IT('id', true, '请选择点赞配置');
        $activity_info = MediaLikeConfig::where(['id' => $id])->first();
        if(empty($activity_info)) {
            return json_error('视频点赞配置数据异常');
        }

        $activity_info->media_type = (int)$activity_info->media_type;
        $activity_info->cycle_type = (int)$activity_info->cycle_type;
        $activity_info->frequency = (int)$activity_info->frequency;
        $activity_info->start_time = time2date($activity_info->start_time);
        $activity_info->end_time = time2date($activity_info->end_time);
        return json_success('ok', $activity_info);
    }

    /**
     * @desc 添加视频评论配置
     */
    public function add_media_comment_config()
    {
        $params = [];
        $params['config_name'] = IT('config_name', true, '请输入配置名称');
        $params['media_type'] = IT('media_type', true, '请选择评论对象');
        $params['frequency'] = IT('frequency', true, '请输入评论的频率');
        $params['cycle_type'] = IT('cycle_type', true, '请输入评论的周期类型');
        $params['cycle_time'] = (int)IT('cycle_time', 1);
        $params['comment_content'] = IT('comment_content', true, '请输入评论的内容');
        $params['start_time'] = IT('start_time', true, '请输入评论的开始时间');
        $params['end_time'] = IT('end_time', true, '请输入评论的结束时间');
        $params['operator_id'] = $this->user['id'];

        $rs = MediaCommentConfigService::add_media_comment_config($params);
        if($rs['code'] != 0) {
            return json_error($rs['msg']);
        }

        return json_success('ok');
    }

    /**
     * @desc 修改视频评论配置
     * @return array
     */
    public function modify_media_comment_config()
    {
        $params = [];
        $params['id'] = IT('id', true, '请选择需要修改的配置');
        $params['config_name'] = IT('config_name', true, '请输入配置名称');
        $params['media_type'] = IT('media_type', true, '请选择评论对象');
        $params['frequency'] = IT('frequency', true, '请输入评论的频率');
        $params['cycle_type'] = IT('cycle_type', true, '请输入评论的周期类型');
        $params['cycle_time'] = (int)IT('cycle_time', 1);
        $params['comment_content'] = IT('comment_content', true, '请输入评论的内容'); // @todo 后面评论内容不需要填写(每个营销的账号对应评论内容)
        $params['start_time'] = IT('start_time', true, '请输入评论的开始时间');
        $params['end_time'] = IT('end_time', true, '请输入评论的结束时间');
        $params['operator_id'] = $this->user['id'];

        $rs = MediaCommentConfigService::modify_media_comment_config($params);
        if($rs['code'] != 0) {
            return json_error($rs['msg']);
        }

        return json_success('ok');
    }

    /**
     * @desc 修改视频评论配置状态
     * @return array
     */
    public function modify_media_comment_config_status()
    {
        $params = [];
        $params['id'] = IT('id', true, '请选择点赞配置');
        $params['status'] = IT('status', true, '点赞配置状态异常');
        $params['operator_id'] = $this->user['id'];

        $rs = MediaCommentConfigService::modify_media_comment_config_status($params);
        if($rs['code'] != 0) {
            return json_error($rs['msg']);
        }

        return json_success('ok');
    }

    /**
     * @desc 获取视频评论配置列表
     * @return array
     */
    public function get_media_comment_config_list(Request $request)
    {
        $page = max(1, (int) $request->input('page'));
        $page_size = (int) $request->input('pageszie');
        $page_size = $page_size ?: 20;
        $offset = ($page - 1) * $page_size;
        $total = MediaCommentConfig::where([])->count();
        $fields = ['id', 'config_name', 'media_type', 'frequency', 'cycle_type', 'status', 'start_time', 'end_time'];
        $config_list = MediaCommentConfig::where([])->skip($offset)->take($page_size)->orderBy('id', 'desc')->get($fields);
        $total_page = ceil($total / $page_size);
        if(!empty($config_list)) {
            foreach ($config_list as &$config) {
                $config['media_type'] = (int)$config['media_type'];
                $config['cycle_type'] = (int)$config['cycle_type'];
                $config['frequency'] = (int)$config['frequency'];
                $config['start_time'] = time2date($config['start_time']);
                $config['end_time'] = time2date($config['end_time']);
            }
        }
        return json_success('ok', ['totalpage' => $total_page,'total'=>$total, 'list' => $config_list]);
    }

    /**
     * @desc 获取视频评论配置详情
     * @return array
     */
    public function get_media_comment_config_info()
    {
        $id = IT('id', true, '请选择评论配置');
        $activity_info = MediaCommentConfig::where(['id' => $id])->first();
        if(empty($activity_info)) {
            return json_error('视频评论配置数据异常');
        }

        $activity_info->start_time = time2date($activity_info->start_time);
        $activity_info->end_time = time2date($activity_info->end_time);
        return json_success('ok', $activity_info);
    }

    /**
     * @desc 获取视频评论的内容列表
     */
    public function get_media_comment_list(Request $request)
    {
        $page = max(1, (int) $request->input('page'));
        $page_size = (int) $request->input('pageszie');
        $page_size = $page_size ?: 100;
        $offset = ($page - 1) * $page_size;
        $where_arr = [];
        $total = MediaCommentTemplate::where($where_arr)->count();
        $fields = ['id', 'comment_content', 'is_delete', 'is_used', 'operator_id', 'created_at', 'updated_at'];
        $template_list = MediaCommentTemplate::where($where_arr)->skip($offset)->take($page_size)->orderBy('id', 'desc')->get($fields);
        $total_page = ceil($total / $page_size);

        return json_success('ok', ['totalpage' => $total_page,'total'=>$total, 'list' => $template_list]);
    }
}